package com.fly.consumers.service;

import java.util.concurrent.TimeUnit;

import org.springframework.stereotype.Service;

import com.fly.consumers.controller.HelloController;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;


@Service
public class SkillService {
	
	public static int enterCount = 0;

	@HystrixCommand(
//			groupKey = "gggg",
//			threadPoolKey = "hystrix-pool-1",
			fallbackMethod = "getSkillFallback",
			commandProperties = {
					@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000"),
					@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "30"),
					@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),
					@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
					@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds",value = "10000"),
			},
			threadPoolProperties = {
					@HystrixProperty(name = "coreSize",value = "9"),
					@HystrixProperty(name = "queueSizeRejectionThreshold",value = "0"),
			})
	public String getSkill2(String id) {
		return "getSkill2";
	}
	
	
	@HystrixCommand(
//			threadPoolKey = "hystrix-pool-2",
			commandKey = "hello",
			fallbackMethod = "getSkillFallback",
			commandProperties = {
					@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000"),
					@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "30"),
					@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),
					@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
					@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds",value = "10000"),
			},
			threadPoolProperties = {
					@HystrixProperty(name = "coreSize",value = "8"),
					@HystrixProperty(name = "queueSizeRejectionThreshold",value = "0"),
			}
			)
	public String getSkill(String id) {
		/** 成功打出这句日志，证明调用方法成功
			1·关闭状态：能多次过打出这个日志
			2·半开状态：能打印出一次这个日志，根据情况切换到关闭或全开
			3·全开状态：不会输出这句日志，证明直接走 fallback 方法，没调用实际方法
		 **/
		System.out.println("------------断路器未开启---------------");
		String threadName = "(" + Thread.currentThread().getName() + ")";
		System.err.println(threadName + " : " + "SkillService.getSkill()");
		// 模拟抛出 exception，开启断路器。
		if(HelloController.hystrixFlag) {
			throw new RuntimeException("throw service error.");
		}
		// 模拟超时高压力测试 ,timeout exception 也算是异常请求，也会触发开启断路器。
		if(HelloController.timeoutFlag) {
			try {
				Thread.sleep(5000L);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return "skill service.";
	}
	
	public String getSkillFallback(String id,Throwable ex) {
		String threadName = "("+Thread.currentThread().getName()+")";
		ex.printStackTrace();
		System.err.println(threadName + " : " + "SkillService.getSkillFallback(" + id + ")");
		return "error";
	}
	
	public String getSkillFallback1(String id,Exception ex) {
		String threadName = "("+Thread.currentThread().getName()+")";
		ex.printStackTrace();
		System.err.println(threadName + " : " + "SkillService.getSkillFallback(" + id + ")");
		return "error";
	}
	
	public String getSkillFallback2(String id) {
		String threadName = "("+Thread.currentThread().getName()+")";
		System.err.println(threadName + " : " + "SkillService.getSkillFallback(" + id + ")");
		return "error";
	}
	
	public String getSkillFallback2() {
		String threadName = "("+Thread.currentThread().getName()+")";
		System.err.println(threadName + " : " + "SkillService.getSkillFallback()");
		return "error";
	}
	
}
